package com.spsy.orders.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Component
@RabbitListener(queues = "lzp_queue_2206a7")//监听的队列名称
public class DirectReceiver3 {

    @Autowired
    private RedisTemplate redisTemplate;


    private ExecutorService executorService = Executors.newFixedThreadPool(3);

    @RabbitHandler
    public void process(String msg, Message message, Channel channel) {
        System.out.println("======>lzp_queue_2206a7 消费者收到消息  : " + msg);

        try {



            Map<String,String> map = JSON.parseObject(msg,Map.class);

            String orderId = map.get("orderId");
            String orderStatus = map.get("orderStatus");

            /*String orderId = (String) map.get("orderId");
            String orderStatus = (String)map.get("orderStatus");

            System.out.println("orderId:"+orderId+" orderStatus:"+orderStatus);*/


            /*executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("线程中执行。。msg:"+msg);
                }

            });*/

            executorService.submit(new Task(orderId,orderStatus));





        } catch (Exception e) {
           e.printStackTrace();
        }
    }

    class Task implements Runnable{

        private String orderId;
        private String orderStatus;

        public Task(String orderId,String orderStatus){
            this.orderId = orderId;
            this.orderStatus = orderStatus;
        }

        @Override
        public void run() {
            System.out.println("线程中执行。。orderId:"+orderId+":orderStatus:"+orderStatus);
        }
    }
}
